/**
 * 
 */
package us.gaaoc.framework.webapp.backingbeans.agent;



import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UISelectOne;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.faces.validator.ValidatorException;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Transactional;

import us.gaaoc.framework.logic.validation.DateValidationLogic;
import us.gaaoc.framework.model.DocumentInstance;
import us.gaaoc.framework.util.io.FileUtility;
import us.gaaoc.framework.util.validators.DateValidator;
import us.gaaoc.framework.webapp.backingbeans.BaseBackingBean;
import us.gaaoc.framework.webapp.util.Messages;

/**
 * @author Rex McElrath
 * Oct 24, 2007
 *
 */
public class CourtCalendarReportPage extends BaseBackingBean {	

	/**
	 * Date of the starting day of Court for the Court Calendar Report to Pull.
	 */
	private Date startCourtDate;
	/**
	 *  Date of the ending day of Court for the Court Calendar Report to Pull.
	 */
	private Date endCourtDate;
	/**
	 * An array list of the different format types available for a report.
	 * Formats available
	 * <ul>
	 * 	<li> PDF </li>
	 * 	<li> RTF </li>
	 * 	<li> PNG </li>
	 */
	private ArrayList<SelectItem> reportFormatTypeDescriptionItems;
	/**
	 * A user interface input item to hold the currently selected report format
	 * variable.
	 */
	private UISelectOne reportFormatTypeInput;
	/**
	 * A string to hold the value of the report format that is selected.
	 */
	private int reportFormatType;
	/**
	 * Java Server Faces Message component to be used to display error and
	 * informational messages.
	 */
	private FacesMessage message;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8905870571068078452L;
	/**
	 * Properties
	 */
	private static final Logger LOG = Logger.getLogger(CourtCalendarReportPage.class);
	
	
	/* *************************************************************************
	 * Navigation Methods
	 * *************************************************************************
	 */
	
	/**
	 * Trigger navigation to the System Statistics Page
	 */
	/*public String goToSystemStatistics() {
		return "goToSystemStatistics";
	}
	/**
	 * Trigger navigation to the Personal Statistics Page
	 */
/*	public String goToPersonalStatistics() {
		return "goToPersonalStatistics";
	}*/
	
	
	/* *************************************************************************
	 * Variable Getter and Setter Section
	 * *************************************************************************
	 */
	/**
	 * @return the startCourtDate
	 */
	public Date getStartCourtDate() {
		return startCourtDate;
	}

	/**
	 * @param startCourtDate the startCourtDate to set
	 */
	public void setStartCourtDate(Date startCourtDate) {
		this.startCourtDate = startCourtDate;
	}

	/**
	 * @return the endCourtDate
	 */
	public Date getEndCourtDate() {
		return endCourtDate;
	}

	/**
	 * @param endCourtDate the endCourtDate to set
	 */
	public void setEndCourtDate(Date endCourtDate) {
		this.endCourtDate = endCourtDate;
	}
	
	/**
	 * @return docTypeDescriptionsItems
	 */
	public ArrayList<SelectItem> getReportFormatTypeDescriptionItems() {
		// Initialize Array List if null
		if (reportFormatTypeDescriptionItems == null) {
			reportFormatTypeDescriptionItems = new ArrayList<SelectItem>();
		
		}
		
			
		reportFormatTypeDescriptionItems.add(new SelectItem(1, 
				"Editable (.RTF)"));
		reportFormatTypeDescriptionItems.add(new SelectItem(2, "PDF"));
		reportFormatTypeDescriptionItems.add(new SelectItem(3, "Image (.PNG)"));
		
		return reportFormatTypeDescriptionItems;
	}

	public void setReportFormatTypDescriptionItems(ArrayList<SelectItem> si) {
		reportFormatTypeDescriptionItems = si;
	}

	/**
	 * @return the reportFormatTypeInput
	 */
	public UISelectOne getReportFormatTypeInput() {
		return reportFormatTypeInput;
	}

	/**
	 * @param reportFormatTypeInput the reportFormatTypeInput to set
	 */
	public void setReportFormatTypeInput(UISelectOne reportFormatTypeInput) {
		this.reportFormatTypeInput = reportFormatTypeInput;
	}

	/**
	 * @return the reportFormatType
	 */
	public int getReportFormatType() {
		return reportFormatType;
	}

	/**
	 * @param reportFormatType the reportFormatType to set
	 */
	public void setReportFormatType(int reportFormatType) {
		this.reportFormatType = reportFormatType;
	}
	
	/* *************************************************************************
	 * Logic Invoking Method Section
	 * *************************************************************************
	 */
	
	/**
	 * Wrapper Method to interface with the date validation utility. 
	 * 
	 * @param context
	 * @param component
	 * @param value
	 * @throws ValidatorException
	 * @throws Exception
	 */
	public void validateDate(FacesContext context, UIComponent component,
			Object value) throws ValidatorException, Exception {
		Date dateToCheck = (Date) value;
		String dateToCheckString = dateToCheck.toString();

		/*
		 * There is a bug in JSF page to pass over a date value, the passed
		 * value is always 1 day behind . The day field of the Calendar object
		 * starts with 1 but the month field starts with 0. This confusion might
		 * be the cause of the bug.
		 */
		DateValidationLogic dvl = new DateValidationLogic();
		Boolean validDate = dvl.validateMMDDYYYYFormatDate(dateToCheckString);
		if (validDate == false) {
			message = Messages.getMessage(
					"us.gaaoc.framework.webapp.util.errorMessage",
					"incorrectDate", null);
			message.setDetail(message.getDetail());
			message.setSeverity(FacesMessage.SEVERITY_ERROR);
			throw new ValidatorException(message);
		} 
	}
	/**
	 * Method to generate a report and stream it to the browser
	 */
	

  public void generateReport() {
	  
		//CourtCalendarReportLogic ccrl = new CourtCalendarReportLogic();
		HttpServletResponse resp = (HttpServletResponse) this.getFacesContext()
		.getExternalContext().getResponse();
		// Set document title and report format parameter based on the type of 
		// document
		String docTitle;
		int reportStreamFormatParameter = 0;
		// Format is RTF
		if (reportFormatType == 1) {
			docTitle = "CourtCalendarReport-Editable";
			reportStreamFormatParameter = 2;
		// Format is PDF
		} else if (reportFormatType == 2) {
			docTitle = "CourtCalendarReport";
			reportStreamFormatParameter = 1;
		// Format is PNG
		}else if (reportFormatType == 3) {
			docTitle = "CourtCalendarReport-Image";
			reportStreamFormatParameter = 3;
		} else {
			docTitle = "CourtCalendarReport";
		} 
		
  }
		// Generate report to get the doc content to stream to the browser
		//byte[]  reportContent = generateCourtCalendarReport (reportFormatType);
		/*
		Map sessionMap = this.getFacesContext().getExternalContext()
		.getSessionMap();
		String courtUuid = (String) sessionMap.get("courtUuid");
		*/
	/*	String courtUuid = "courtUuid";
		
		byte[]  reportContent = ccrl.generateCourtCalendarReport(courtUuid, startCourtDate, endCourtDate, reportFormatType);
		// Pass Report out to the browser
		try {
			FileUtility.streamReportToBrowser(resp, docTitle, reportContent,
					reportStreamFormatParameter);
		} catch (IOException e) {
			LOG.error("Error in CourtCalendar.generateReport() method: " + e);
			// throw e;
		}
	
	}*/
	/*public String  generateReport(){
		return "stay";
    */
	public String back() {
		return "back";
	}
	
}
